<template>
  <div id="add">
    <el-dialog
      :title="obj.isadd ? '分类添加' : '分类修改'"
      :visible.sync="obj.istrue"
      width="50%"
      @close="cancel()"
      @opened="opened()"
    >
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        style="width: 75%; background: #fff"
      >
        <el-form-item label="一级分类">
          <el-select
            v-model="form.first_cateid"
            placeholder="请选择一级分类"
            @change="change"
          >
            <el-option
              v-for="item in catelists"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select v-model="form.second_cateid" placeholder="请选择二级分类">
            <el-option
              v-for="item in secondarr"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="form.goodsname"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="市场价格">
          <el-input v-model="form.market_price"></el-input>
        </el-form-item>
        <el-form-item label="上传图片">
          <div class="filebox">
            <h2>+</h2>
            <img :src="imgURL" alt="" v-if="imgURL" />
            <input type="file" @change="changeimg" />
          </div>
        </el-form-item>
        <el-form-item label="商品规格">
          <el-select
            v-model="form.specsid"
            placeholder="请选择一级分类"
            @change="changeattr"
          >
            <el-option
              v-for="item in specslists"
              :key="item.id"
              :label="item.specsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格属性">
          <el-select
            v-model="form.specsattr"
            multiple
            placeholder="请选择一级分类"
          >
            <el-option
              v-for="(item, index) in secondattrs"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否新品">
          <el-radio :label="1" v-model="form.isnew">是</el-radio>
          <el-radio :label="2" v-model="form.isnew">否</el-radio>
        </el-form-item>
        <el-form-item label="是否热卖">
          <el-radio :label="1" v-model="form.ishot">是</el-radio>
          <el-radio :label="2" v-model="form.ishot">否</el-radio>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
        <el-form-item label="商品描述">
          <div id="div1" v-if="obj.istrue"></div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="submitadd" v-if="obj.isadd"
          >确 定</el-button
        >
        <el-button type="primary" v-else @click="edit">修改</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import E from "wangeditor";
import { successAlert, warningAlert } from "../../../util/alert.js";
import { mapActions, mapGetters } from "vuex";
import { catelist, goodsadd, goodsinfo, goodsedit } from "../../../requset/api";
export default {
  props: ["obj"],
  data() {
    return {
      editor: "",
      imgURL: "",
      secondarr: [],
      secondattrs: [],
      form: {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        specsid: "",
        specsattr: "",
        isnew: 1,
        ishot: 1,
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      catelists: "cate/list",
      specslists: "specs/list",
    }),
  },
  methods: {
    ...mapActions({
      reqChangeGoods: "goods/reqChangeGoods",
      reqChangeTotal: "goods/reqChangeTotal",
      reqChangeCate: "cate/reqChangeCate",
      reqChangeSpecs: "specs/reqChangeSpecs",
    }),
    emty() {
      this.form = {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        specsid: "",
        specsattr: "",
        isnew: 1,
        ishot: 1,
        status: 1,
      };
      this.imgURL = "";
    },
    cancel() {
      (this.obj.istrue = false), (this.obj.isadd = true), this.emty();
    },
    opened() {
      this.editor = new E('#div1');
      this.editor.create();
      this.editor.txt.html(this.form.description)
    },
    submitadd() {
      let data={
        ...this.form,
        description:this.editor.txt.html(),
        specsattr:JSON.stringify(this.form.specsattr)
      }
      goodsadd(data).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqChangeGoods(), this.reqChangeTotal();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    getone(id) {
      goodsinfo({ id: id }).then((res) => {
        (this.form = res.data.list),
          catelist({ pid: this.form.first_cateid }).then((res) => {
            this.secondarr = res.data.list;
          });
        this.form.specsattr = JSON.parse(this.form.specsattr);
        let data = this.specslists.find((item) => item.id == this.form.specsid);
        this.secondattrs = data.attrs;
        (this.imgURL = this.$pre + res.data.list.img), (this.form.id = id);
      });
    },
    edit() {
      let data={
        ...this.form,
        description:this.editor.txt.html(),
        specsattr:JSON.stringify(this.form.specsattr)
      }
      goodsedit(data).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqChangeGoods();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    change() {
      catelist({ pid: this.form.first_cateid }).then((res) => {
        this.secondarr = res.data.list;
        this.form.second_cateid = "";
      });
    },
    changeattr() {
      let data = this.specslists.find((item) => item.id == this.form.specsid);
      this.secondattrs = data.attrs;
      this.form.specsattr = "";
    },
    changeimg(e) {
      let file = e.target.files[0];
      if (file.size >= 2 * 1024 * 1024) {
        warningAlert("图片超出规定的大小，请上传小于2M的图片");
      } else {
        this.imgURL = URL.createObjectURL(file);
        this.form.img = file;
      }
    },
  },
  mounted() {
    this.reqChangeCate(), this.reqChangeSpecs(true);
  },
};
</script>
<style lang="less" scoped>
.filebox {
  height: 200px;
  width: 200px;
  border: 1px dashed #cecece;
  position: relative;
  h2 {
    line-height: 200px;
    font-size: 30px;
    font-weight: bold;
    text-align: center;
  }
  input {
    height: 200px;
    width: 200px;
    position: absolute;
    left: 0;
    top: 0;
    opacity: 0;
  }
  img {
    height: 200px;
    width: 200px;
    position: absolute;
    left: 0;
    top: 0;
  }
}
</style>